unit uDBLoader;

interface
uses
  Windows,SysUtils,Classes,SQLiteTable3,uLoaderbase,uGlobal;
const
  C_SQL_HISTORY='Select * From history order by issue ASC';
type
  TDBLoader=class(TLoaderBase)
  private
    FDBPath:string;
    Fsldb:TSQLiteDatabase;
    FslTable:TSQLiteTable;
  public
    constructor Create(ADBPath:string);
    destructor Destroy;

    procedure LoadBallList;override;
    procedure AddNumbers(AItem:Pointer);override;
    procedure DeleteNumber(AIndex:integer);overload; override;
    procedure DeleteNumber(AItem:Pointer);overload;
    procedure UpdateNumber(AItem:Pointer);override;

    procedure InsertHotDegree(AtbHD:TtbHotDegree);override;
  end;
implementation
uses
  uFuncs;

{ TDBLoad }

procedure TDBLoader.AddNumbers(AItem: Pointer);
const
  C_SQL='insert into History(opendate,issue,%s,blue) values("%s",%D,%s,%D)';
var
  PNum:PNumInfo;
  sSql,sRedBalls,sRedValues:string;

begin
  PNum:=AItem;
  sRedBalls:=JoinMultiStr('red',1,6);
  sRedValues:=ArrToStr(PNum^.RedBalls);
  sSql:=Format(C_SQL,[sRedBalls,
                      FormatDatetime('yyyy/mm/dd',PNum^.OpenDate),
                      PNum^.Issue,
                      sRedValues,
                      PNum^.BlueBall]);
  try
    Fsldb.ExecSQL(sSql);
  finally
  end;

end;

constructor TDBLoader.Create(ADBPath: string);
begin
  inherited Create;

  FDBPath:=ADBPath;
  Fsldb:=TSQLiteDatabase.Create(FDBPath);
  FslTable:=Fsldb.GetTable(C_SQL_HISTORY);
end;

procedure TDBLoader.DeleteNumber(AIndex: integer);
begin
  inherited;

end;

procedure TDBLoader.DeleteNumber(AItem: Pointer);
begin

end;

destructor TDBLoader.Destroy;
begin
  if Assigned(Fsldb) then
    Fsldb.Free;
  if Assigned(FslTable) then
    FslTable.Free;
end;

procedure TDBLoader.InsertHotDegree(AtbHD:TtbHotDegree);
const
  C_SQL='Insert into T_HotValue(issue,degrees,sorteddegree,%0:s,%1:s) values(%2:d,"%3:s","%4:s",%5:s,%6:s)';
var
  sSql,sDegrees,sStDegrees,sRedRank,sRedDegree,sRankValues,sDegreeValues:string;
  i:integer;
begin
  sRedRank:=JoinMultiStr('red_rank',1,6);
  sRedDegree:=JoinMultiStr('red_degree',1,6);
  sDegrees:=ArrToStr(AtbHD.degrees);
  sStDegrees:=ArrToStr(AtbHd.sortedDegrees);
  sRankValues:=ArrToStr(AtbHD.red_ranks);
  sDegreeValues:=ArrToStr(AtbHD.red_degrees);

  sSql:=Format(C_SQL,[sRedRank,sRedDegree,AtbHD.issue,sDegrees,sStDegrees,sRankValues,sDegreeValues]);
  try
    FslDB.ExecSQL(sSql);
    except

    end;
end;

procedure TDBLoader.LoadBallList;
var
  i:Integer;
  PNum:PNumInfo;
  sDate:string;
begin
  if not Assigned(BallList) then
    BallList:=TList.Create;
  BallList.Clear;

  while not FslTable.EOF do
  begin
    New(PNum);
    sDate:=FslTable.FieldAsString(FslTable.FieldIndex['opendate']);
    PNum^.OpenDate:=StrToDateTime(sDate);
    PNum^.Issue:=FslTable.FieldAsInteger(FslTable.FieldIndex['issue']);
    PNum^.BlueBall:=FslTable.FieldAsInteger(FslTable.FieldIndex['blue']);

    for i := 0 to 5 do
      PNum^.RedBalls[i]:=FslTable.FieldAsInteger(FslTable.FieldIndex['red'+inttostr(i+1)]);

    BallList.Add(PNum);
    FslTable.Next;
  end;
end;

procedure TDBLoader.UpdateNumber(AItem: Pointer);
begin
  inherited;

end;

end.
